
const puppeteer = require('puppeteer');
const replaceData = require("./mod/replaceData.js");
const allData = require("./dist/all.js");
const startTime = new Date().getTime();

(async () => {
  // const browser = await puppeteer.launch({
  //   headless: true,
  //   executablePath: process.env.PUPPETEER_EXEC_PATH,
  //   args: ["--no-sandbox", "--disable-gpu", "--disable-dev-shm-usage"],
  // });
  const browser = await puppeteer.launch({
    headless: false, // 设置true则不显示浏览器
    defaultViewport: { width: 1152, height: 508 },
    executablePath: process.env.PUPPETEER_EXEC_PATH,
    // --disable-web-security 禁用CORS策略
    args: ["--no-sandbox", "--disable-gpu", "--disable-dev-shm-usage", '--start-maximized', '--disable-web-security'],
  });
  const page = await browser.newPage();

  // 批量渲染截图
  for (let i = 0; i < allData.length; i++) {
    await replaceData(JSON.stringify(allData[i]), __dirname);
    const pagePath = __dirname + '/dist/index.html';
    await page.goto("file:///" + pagePath.replace("\\", "\\\\"), {
      timeout: 5000,
      waitUntil: ['networkidle0'],
    }).then(() => {
      console.log('已打开：' + pagePath);
    }).catch(err => {
      console.log('打开网页出错:' + pagePath);
    });
    // page.on('dialog', async dialog => {
    //   await dialog.accept();
    // });
    const imgData = await page.screenshot({ path: __dirname + `\\img\\${allData[i].id}.png` });
    console.log(imgData, '--图片数据流--')
  }
  
  await page.close();
  await browser.close();

  const endTime = new Date().getTime();
  console.log('本次执行时间：' + (endTime - startTime) / 1000 + 's');
  //  page.once('load', () => console.log('Page loaded!'));
})();
